package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysPart;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysRole;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUserRoleConn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysPartService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysRoleService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysUserRoleConnService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户角色业务层
 * @author xiaoxu
 * @date 2023/2/07 15:00
 * @version 1.0
 * @since 1.0
 */
@Component
public class RoleBu {
    @Autowired
    private ISysUserRoleConnService sysUserRoleConnService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysPartService sysPartService;
//    拿到角色值
    public String getRole(String userId){
        QueryWrapper<SysUserRoleConn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID",userId);
//        getone
        SysUserRoleConn sysUserRoleConn = sysUserRoleConnService.getOne(queryWrapper);
        QueryWrapper<SysRole> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID",sysUserRoleConn.getRoleId());
//        getone
        SysRole sysRole = sysRoleService.getOne(queryWrapper1);
        return sysRole.getRoleCode();
    }
//拿到所有角色
    public SzResult getAllRole(String roleName){
       QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(roleName !=null && !roleName.isEmpty(),"ROLE_NAME",roleName);
         List<SysRole> sysRoleList = sysRoleService.list(queryWrapper);
        return SzResult.success(sysRoleList);
    }
//    拿到角色下属的部门
    public SzResult getRolePart(String roleId){
        QueryWrapper<SysPart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PART_ROLE",roleId);
        List<SysPart> sysPartList = sysPartService.list(queryWrapper);
        return SzResult.success(sysPartList);
    }
}
